class Sales::ContactsController < ApplicationController
  before_action :set_sales_contact, only: [:show, :edit, :update, :destroy]

  # GET /sales/contacts
  # GET /sales/contacts.json
  def index
    @sales_contacts = Sales::Contact.all
  end

  # GET /sales/contacts/1
  # GET /sales/contacts/1.json
  def show
    @html_title = '联系人详情'
  end

  # GET /sales/contacts/new
  def new
    @html_title = '新增联系人'
    @sales_contact = Sales::Contact.new(firm_id: params[:firm_id])
    render layout: 'modal'
  end

  # GET /sales/contacts/1/edit
  def edit
    @html_title = '修改联系人信息'
    render layout: 'modal'
  end

  # POST /sales/contacts
  # POST /sales/contacts.json
  def create
    @sales_contact = Sales::Contact.new(sales_contact_params)
    respond_to do |format|
      if @sales_contact.save
        show_js_notice '创建成功'
        format.json { render :show, status: :created, location: @sales_contact }
      else
        format.html { render :new }
        format.json { render json: @sales_contact.errors, status: :unprocessable_entity }
        format.js { error_notice @sales_contact }
      end
    end
  end

  # PATCH/PUT /sales/contacts/1
  # PATCH/PUT /sales/contacts/1.json
  def update
    respond_to do |format|
      if @sales_contact.update(sales_contact_params)
        format.js { show_js_notice '修改成功' }
        format.html { show_js_notice '修改成功' }
      else
        format.html { render :edit }
        format.json { render json: @sales_contact.errors, status: :unprocessable_entity }
        format.js { error_notice @sales_contact }
      end
    end
  end

  # DELETE /sales/contacts/1
  # DELETE /sales/contacts/1.json
  def destroy
    @sales_contact.destroy
    respond_to do |format|
      format.html { redirect_to sales_contacts_url, notice: 'Contact was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_sales_contact
      @sales_contact = Sales::Contact.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def sales_contact_params
      params.require(:sales_contact).permit(:firm_id, :name, :email, :phone, :phone_ext, :phone_same_with_firm, :mobile, :notes, :active, :position, :salutation, :key_person)
    end
end
